const app = getApp();
const douban = app.globalData.doubanUrl;
import utils from "../../utils/utils";
const http = utils.http;
const sliceTitle = utils.sliceTitle;
const director = utils.director;
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },
  onConfirm(e){
    var value = e.detail.value;
    var url = `https://douban.uieee.com/v2/movie/search?q=${value}`;
    http(url,this.handleData);
  },
  handleData(res){
    var subjects = res.data.subjects;
    var movies = [];
    subjects.forEach(ele=>{
      var temp ={};
      var title = sliceTitle(ele.title);
      var id = ele.id;
      var average = ele.rating.average;
      var imgUrl =ele.images.small;
      var originalTitle = ele.original_title;
      var directors = director(ele.directors[0]);
      var temp = {
        title,
        id,
        average,
        imgUrl,
        originalTitle,
        directors,
      }
      movies.push(temp);
    })
    this.setData({
      movies
    })
  },
  onList(event){
    var id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/index/movies-detail/movies-detail?id='+id
    })
  },
  onReachBottom() {
    var value = this.data.value;
    console.log(value);
    var start = this.data.start;
    start += 20;
    this.setData({
      start
    })
    var url = `${douban}search?q=${value}&start=${start}&count=20`;
    http(url, this.handleData);
    wx.showLoading({
      title: "加载数据"
    });
  }
})